package com.gusto.engine.spring.parsers;

import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import com.gusto.engine.semsim.measures.impl.ComposedSimilarity;
import com.gusto.engine.semsim.measures.impl.ComposedSimilarityParam;

public class ParserComposedSimilarity extends AbstractBeanDefinitionParser {
	
	private BeanDefinitionBuilder parseProperty(Element element, ParserContext parserContext) {
      BeanDefinitionBuilder property = BeanDefinitionBuilder.rootBeanDefinition(String.class);
      BeanDefinition bd = parserContext.getDelegate().parseCustomElement(element);
      
      property.addConstructorArgValue(bd);
      return property;
   }
	
	private AbstractBeanDefinition parseComposedParam(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(ComposedSimilarityParam.class);
		
		if (element.hasAttribute("type")) {
			String type = element.getAttribute("type");
			beanDefinitionBuilder.addPropertyValue("type", type);
		}
		if (element.hasAttribute("weight")) {
			String weight = element.getAttribute("weight");
			beanDefinitionBuilder.addPropertyValue("weight", weight);
		}
		if (element.hasAttribute("similarity")) {
			String similarity = element.getAttribute("similarity");
			beanDefinitionBuilder.addPropertyReference("similarity", similarity);
		}
		if (element.hasAttribute("property")) {
			String property = element.getAttribute("property");
			beanDefinitionBuilder.addPropertyValue("property", property);
		}
		
		Element property = DomUtils.getChildElementByTagName(element, "property");
		if (property != null) {
			BeanDefinitionBuilder child = parseProperty(property, parserContext);
			beanDefinitionBuilder.addPropertyValue("property", child.getBeanDefinition());
		}
		return beanDefinitionBuilder.getBeanDefinition();
	}
	
	@SuppressWarnings("unchecked")
	protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(ComposedSimilarity.class);
		
		List childElements = DomUtils.getChildElementsByTagName(element, "composedParam");
		//List<ComposedSimilarityParam> params = new ArrayList<ComposedSimilarityParam>();
		ManagedList children = new ManagedList(childElements.size());
		
		for (int i = 0; i < childElements.size(); ++i) {
	         Element childElement = (Element) childElements.get(i);
	         BeanDefinition child = parseComposedParam(childElement, parserContext);
	         children.add(child);
	    }
		beanDefinitionBuilder. addPropertyValue("params", children);
		return beanDefinitionBuilder.getBeanDefinition();
	}
	
	/*
	private BeanDefinition parseParam(Element element, ParserContext parserContext) {
	      //BeanDefinitionBuilder param = BeanDefinitionBuilder.rootBeanDefinition(ComposedSimilarityParam.class);
	      //component.addPropertyValue("name", element.getAttribute("name"));
	      //Element b = DomUtils.getChildElementByTagName(element, "bean");
	      //BeanDefinition bd = parserContext.getDelegate().parseBeanDefinitionElement(element).getBeanDefinition();
	      //BeanDefinition bd = (new SemSimNamespaceHandler()).parse(element, parserContext);
	      // Good
	      //BeanDefinition bd = parserContext.getDelegate().parseCustomElement(element);
	      
	      BeanDefinition bd = parseComposedParam(element, parserContext);
	      //BeanDefinition bd = parserContext.getDelegate().parseCustomElement(element);
	      
	      return bd;
	   }
	*/
	
}
